package controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.BoardDAO;

import org.apache.log4j.Logger;

import vo.BoardVO;

public class WriteAction implements Action {
	private static Logger logger = Logger.getLogger(LoginAction.class);
	
	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		BoardDAO boardDAO = BoardDAO.getInstance();
		BoardVO boardVO = new BoardVO();
		
		String path = "";
		
		HttpSession session = request.getSession(false);
		String brdUser = (String)session.getAttribute("userid");

		String brdTitle = request.getParameter("brdtitle");
		String brdContent = request.getParameter("brdcontent");
		
		boardVO.setBrdUser(brdUser);
		boardVO.setBrdTitle(brdTitle);
		boardVO.setBrdContent(brdContent);
		logger.debug("params: " + boardVO);
		
		int result = boardDAO.addPost(boardVO);
		if(result != 0) {
			path = "/list.do";
		} else {
			request.setAttribute("errormsg", "글쓰기 에러");
			path = "/error/error.jsp";
		}
		return new ActionForward(path, false);
	}

}
